package dev.ece.suit.ui;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;

import dev.ece.core.texture.Texture;
import dev.ece.suit.text.RangeColorText;
import dev.ece.suit.text.TextLibrary;
import dev.ece.util.lib.IntOffset;
import dev.ece.util.lib.IntRectangle;
import dev.ece.util.lib.Rectangle;
import dev.ece.util.lib.Vector3f;
import dev.ece.util.lib.Vector4f;
import dev.ece.util.obj.ObjectCount;
import dev.ece.util.tools.Camera;

public class TextArea extends ScrollPane<TextContainer> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -317715562173006503L;

	private TextContainer textContainer;
	
	private Vector4f fontColor = new Vector4f(1, 1, 1, 1);
	
	public TextArea(String background, float width, float height, IntRectangle centerClipping,
			ScrollBar horizontalScrollBar, ScrollBar verticalScrollBar) throws IOException {
		super(background, width, height, centerClipping, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(String background, float width, float height, IntRectangle centerClipping, ScrollBar scrollBar)
			throws IOException {
		super(background, width, height, centerClipping, scrollBar);
	}

	public TextArea(String background, float width, float height, IntOffset centerClipping,
			ScrollBar horizontalScrollBar, ScrollBar verticalScrollBar) throws IOException {
		super(background, width, height, centerClipping, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(String background, float width, float height, IntOffset centerClipping, ScrollBar scrollBar)
			throws IOException {
		super(background, width, height, centerClipping, scrollBar);
	}
	
	public TextArea(String background, float width, float height, ScrollBar horizontalScrollBar,
			ScrollBar verticalScrollBar) throws IOException {
		super(background, width, height, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(String background, float width, float height, ScrollBar scrollBar) throws IOException {
		super(background, width, height, scrollBar);
	}

	public TextArea(String background, IntRectangle textureClipping, float width, float height,
			IntRectangle centerClipping, ScrollBar horizontalScrollBar, ScrollBar verticalScrollBar)
			throws IOException {
		super(background, textureClipping, width, height, centerClipping, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(String background, IntRectangle textureClipping, float width, float height,
			IntRectangle centerClipping, ScrollBar scrollBar) throws IOException {
		super(background, textureClipping, width, height, centerClipping, scrollBar);
	}

	public TextArea(String background, IntRectangle textureClipping, float width, float height,
			IntOffset centerClipping, ScrollBar horizontalScrollBar, ScrollBar verticalScrollBar)
			throws IOException {
		super(background, textureClipping, width, height, centerClipping, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(String background, IntRectangle textureClipping, float width, float height,
			IntOffset centerClipping, ScrollBar scrollBar) throws IOException {
		super(background, textureClipping, width, height, centerClipping, scrollBar);
	}
	
	public TextArea(String background, IntRectangle textureClipping, float width, float height,
			ScrollBar horizontalScrollBar, ScrollBar verticalScrollBar) throws IOException {
		super(background, textureClipping, width, height, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(String background, IntRectangle textureClipping, float width, float height, ScrollBar scrollBar)
			throws IOException {
		super(background, textureClipping, width, height, scrollBar);
	}

	public TextArea(ObjectCount<Texture> background, float width, float height, IntRectangle centerClipping,
			ScrollBar horizontalScrollBar, ScrollBar verticalScrollBar) throws IOException {
		super(background, width, height, centerClipping, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(ObjectCount<Texture> background, float width, float height, IntRectangle centerClipping, ScrollBar scrollBar)
			throws IOException {
		super(background, width, height, centerClipping, scrollBar);
	}

	public TextArea(ObjectCount<Texture> background, float width, float height, IntOffset centerClipping,
			ScrollBar horizontalScrollBar, ScrollBar verticalScrollBar) throws IOException {
		super(background, width, height, centerClipping, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(ObjectCount<Texture> background, float width, float height, IntOffset centerClipping, ScrollBar scrollBar)
			throws IOException {
		super(background, width, height, centerClipping, scrollBar);
	}
	
	public TextArea(ObjectCount<Texture> background, float width, float height, ScrollBar horizontalScrollBar,
			ScrollBar verticalScrollBar) throws IOException {
		super(background, width, height, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(ObjectCount<Texture> background, float width, float height, ScrollBar scrollBar) throws IOException {
		super(background, width, height, scrollBar);
	}

	public TextArea(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height,
			IntRectangle centerClipping, ScrollBar horizontalScrollBar, ScrollBar verticalScrollBar)
			throws IOException {
		super(background, textureClipping, width, height, centerClipping, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height,
			IntRectangle centerClipping, ScrollBar scrollBar) throws IOException {
		super(background, textureClipping, width, height, centerClipping, scrollBar);
	}

	public TextArea(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height,
			IntOffset centerClipping, ScrollBar horizontalScrollBar, ScrollBar verticalScrollBar)
			throws IOException {
		super(background, textureClipping, width, height, centerClipping, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height,
			IntOffset centerClipping, ScrollBar scrollBar) throws IOException {
		super(background, textureClipping, width, height, centerClipping, scrollBar);
	}
	
	public TextArea(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height,
			ScrollBar horizontalScrollBar, ScrollBar verticalScrollBar) throws IOException {
		super(background, textureClipping, width, height, horizontalScrollBar, verticalScrollBar);
	}

	public TextArea(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height, ScrollBar scrollBar)
			throws IOException {
		super(background, textureClipping, width, height, scrollBar);
	}
	
	@Override
	protected void setupContent() {
		super.setupContent();
		this.textContainer = createTextContainer();
		this.addContentItem(getTextContainer());
	}

	protected TextContainer getTextContainer() {
		return textContainer;
	}

	protected TextContainer createTextContainer() {
		return new TextContainer(1, 1);
	}

	public Vector4f getFontColor() {
		return fontColor;
	}

	public void setFontColor(Vector4f fontColor) {
		if(fontColor == null) {
			throw new IllegalArgumentException("fontColor不能为null");
		}
		this.fontColor = fontColor;
		if(getTextContainer().getRangeColorText() != null) {
			getTextContainer().getRangeColorText().getDefaultColor().set(getFontColor());
		}
	}

	public void setText(String text) {
		getTextContainer().setText(text);
		if(getTextContainer().getRangeColorText() != null) {
			getTextContainer().getRangeColorText().getDefaultColor().set(getFontColor());
		}
		computerTextContainerPosition();
	}
	
	public void getText() {
		getTextContainer().getText();
	}
	
	public void setFontSize(int fontSize) {
		getTextContainer().setFontSize(fontSize);
		computerTextContainerPosition();
	}
	
	public int getFontSize() {
		return getTextContainer().getFontSize();
	}
	
	public void setTextLibrary(TextLibrary textLibrary) {
		getTextContainer().setTextLibrary(textLibrary);
		computerTextContainerPosition();
	}

	protected void computerTextContainerPosition() {
		Rectangle viewer = getContentViewRectangle();
		getTextContainer().position.set(
				viewer.left + getTextContainer().getWidth() / 2, 
				viewer.top - getTextContainer().getHeight() / 2);
		computeContentSize();
	}
	
}
class TextContainer extends Container {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8111551000355851652L;
	
	private String text;
	
	private RangeColorText rangeColorText;
	
	private int fontSize = 15;
	
	private TextLibrary textLibrary = Fonts.getSystemTextLibrary();
	
	private final Vector3f textPosition = new Vector3f();
	
	public TextContainer(float width, float height) {
		super(width, height);
	}

	@Override
	public void onRender(Camera camera) {
		if(getRangeColorText() != null) {
			getTextLibrary().renderColorText(getRangeColorText(), getFontSize(), getTextPosition());
		}
	}
	
	protected void computeTextSizeAndPostion() {
		if(StringUtils.isBlank(getText())) {
			this.setWidth(0);
			this.setHeight(0);
			getTextPosition().set(0, 0);
		} else {
			int len = getRangeColorText().getLines().length;
			int width = 0;
			for(int i=0; i<len; i++) {
				width = Math.max(width, (int) getTextLibrary().getCharsWidth(getRangeColorText().getLines()[i].getText(), getFontSize()));
			}
			setWidth(width);
			setHeight((int) getTextLibrary().getHeight(getFontSize(), getRangeColorText()));
			getTextPosition().set(- getWidth() / 2, getHeight() / 2);
		}
	}

	protected void setText(String text) {
		if(textLibrary == null) {
			throw new IllegalArgumentException("textLibrary不能为null");
		}
		this.text = text;
		this.rangeColorText = StringUtils.isNotBlank(getText()) ? getTextLibrary().createTangeColorText(getText()) : null;
		this.computeTextSizeAndPostion();
	}
	
	protected String getText() {
		return text;
	}

	protected int getFontSize() {
		return fontSize;
	}

	protected void setFontSize(int fontSize) {
		if(fontSize <= 0) {
			throw new IllegalArgumentException("fontSize必须大于0");
		}
		if(textLibrary == null) {
			throw new IllegalArgumentException("textLibrary不能为null");
		}
		this.fontSize = fontSize;
		this.computeTextSizeAndPostion();
	}

	protected TextLibrary getTextLibrary() {
		return textLibrary;
	}

	protected void setTextLibrary(TextLibrary textLibrary) {
		if(textLibrary == null) {
			throw new IllegalArgumentException("textLibrary不能为null");
		}
		this.textLibrary = textLibrary;
		this.rangeColorText = StringUtils.isNotBlank(getText()) ? getTextLibrary().createTangeColorText(getText()) : null;
		this.computeTextSizeAndPostion();
	}

	protected RangeColorText getRangeColorText() {
		return rangeColorText;
	}

	protected Vector3f getTextPosition() {
		return textPosition;
	}

}